package com.tdam2013.grupo12;

import java.util.Calendar;

import com.tdam2013.grupo12.adapters.WidgetService;
import com.tdam2013.grupo12.broadcastReceiver.MyWidgetService;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.SystemClock;
import android.widget.RemoteViews;

public class MiWidget extends AppWidgetProvider {

	private static String LLAMADA_ACTION = "LLAMADA";
	public static String UPDATE = "UPDATE";
	public static String EXTRA_ITEM = "EXTRA";
	private PendingIntent service = null; 
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		for (int i = 0; i < appWidgetIds.length; i++) {
			Intent intent = new Intent(context, WidgetService.class);
			intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
					appWidgetIds[i]);
			intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

			final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  
			  
	        final Calendar TIME = Calendar.getInstance();  
	        TIME.set(Calendar.MINUTE, 0);  
	        TIME.set(Calendar.SECOND, 0);  
	        TIME.set(Calendar.MILLISECOND, 0);  
	  
	        final Intent in = new Intent(context, MyWidgetService.class);  
	  
	        if (service == null)  
	        {  
	            service = PendingIntent.getService(context, 0, in, PendingIntent.FLAG_CANCEL_CURRENT);  
	        }  
	  
	        m.setRepeating(AlarmManager.RTC, TIME.getTime().getTime(), 1000 * 2, service); 

			RemoteViews rv = new RemoteViews(context.getPackageName(),
					R.layout.miwidget);

			rv.setRemoteAdapter(appWidgetIds[i], R.id.listaLlamadas, intent);

			Intent toastIntent = new Intent(context, MiWidget.class);
			// Set the action for the intent.
			// When the user touches a particular view, it will have the effect
			// of
			// broadcasting TOAST_ACTION.
			toastIntent.setAction(MiWidget.LLAMADA_ACTION);
			toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
					appWidgetIds[i]);
			intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
			PendingIntent toastPendingIntent = PendingIntent.getBroadcast(
					context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
			rv.setPendingIntentTemplate(R.id.listaLlamadas, toastPendingIntent);
			ComponentName component = new ComponentName(context, MiWidget.class);
			appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds,
					R.id.listaLlamadas);
			appWidgetManager.updateAppWidget(component, rv);
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		AppWidgetManager mgr = AppWidgetManager.getInstance(context);
		if (intent.getAction().equals(LLAMADA_ACTION)) {
			int appWidgetId = intent.getIntExtra(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			String telefono = intent.getStringExtra(EXTRA_ITEM);
			Intent i = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ telefono));
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(i);
		} else if (intent.getAction().equals("Widgetupdate")) {
			ComponentName watchWidget = new ComponentName(context, MiWidget.class);
			RemoteViews views = new RemoteViews(context.getPackageName(),
					R.layout.miwidget);
			(AppWidgetManager.getInstance(context)).updateAppWidget(
					watchWidget, views);
		}
		super.onReceive(context, intent);
	}
}